#!/bin/bash

echo "**************************************************"
echo -e "\n运行$0 $@"

prj_dir=$(cd "$(dirname "$0")"; pwd)
echo "当前目录：${prj_dir}"

# 编译运行后进程列表
source ${prj_dir}/project.conf
echo -n "app_list: "
for app in ${app_list[*]}
do
 	echo -n "${app} "
done
echo ""

code_compile(){
	
	for(( i=0;i<${app_num};i++))
	do
		app_dir=${prj_dir}"/code/"${app_local_folder_list[i]}
		if [ -d ${app_dir} ]
		then
			echo -e "\n ${app_dir} 代码编译"
			cd ${app_dir}
			make $1

		else
			echo "error: path does not exist ${app_dir}"
			
		fi
		unset app_dir
	done
}

# 1.编译，复制可执行程序
code_compile $*

# 2.打包
cd ${prj_dir}/pack
./${prj_pack_sh}
cd ${prj_dir}

#询问是否上传
read -r -p "Upload? [Y/n] " input

case ${input} in
    [yY][eE][sS]|[yY])
        ifupload=1
    ;;

    [nN][oO]|[nN])
		echo "Exit"
		ifupload=0
		#exit 1
    ;;

    *)
	    echo "Exit, invalid input..."
	    ifupload=0
	    #exit 1
    ;;
esac

if [ ${ifupload} -eq 1 ]
then
	device_ip="192.168.0.105"
	# 这里初次使用先建立密钥（不使用sshpass命令），再免密传输（使用sshpass命令）
	send_command="sshpass -p 1212 scp ${prj_dir}/pack/${pack_name} ll@${device_ip}:/tmp"
	# 3.上传
	echo "上传 install_${VER}.bin 到设备的 /tmp 目录"
	# 重置
	# ssh-keygen -R ${device_ip}
	${send_command}

	# rm -rf ${prj_dir}/pack/${pack_name}
	echo "上传结束 $?"
	
fi
echo -e "\n**************************************************"
